package de.uni.muenster.idw2.verbal.graph;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class INScanner {
	InputStreamReader isreader;
	char currentChar;
	
	public INScanner(InputStream file) throws FileNotFoundException, IOException{
		this.isreader = new InputStreamReader(file);
		currentChar=(char)this.isreader.read();
	}
	
	public int nextInt() throws IOException, Exception{
		int num=0;
		
		if(this.isSeparator(currentChar)){
			while(this.isSeparator(currentChar))
				this.currentChar=(char)this.isreader.read();
		}
		
		if(Character.isDigit(currentChar)){
			num=Character.getNumericValue(currentChar);
			currentChar=(char)this.isreader.read();
			while(Character.isDigit(currentChar)){
				num=(num*10)+Character.getNumericValue(currentChar);
				currentChar=(char)this.isreader.read();
			}
			return num;
		}
		else
			throw new Exception("No numeric character currentChar="+currentChar);
	}
	
	public void nextLine() throws IOException, Exception{
		if(this.isSeparator(currentChar)){
			while(this.isSeparator(currentChar)){
				this.currentChar=(char)this.isreader.read();
			}
		}

		if(currentChar=='\r' || currentChar=='\n'){
			while(currentChar=='\r' || currentChar=='\n'){
				currentChar=(char)this.isreader.read();
			}
		}
		else
			throw new Exception("No newline character currentChar="+currentChar);
	}
	
	public String next() throws IOException, Exception{
		if(this.isSeparator(currentChar)){
			while(this.isSeparator(currentChar)){
				this.currentChar=(char)this.isreader.read();
			}
		}
		StringBuilder sb = new StringBuilder();
		if(!Character.isWhitespace(currentChar)){
			while(!Character.isWhitespace(currentChar)){
				sb.append(currentChar);
				currentChar=(char)this.isreader.read();
			}
			return sb.toString();
		}
		else
			throw new Exception("No non whitespace character");
	}
	
	public double nextDouble() throws IOException, Exception{
		if(this.isSeparator(currentChar)){
			while(this.isSeparator(currentChar)){
				System.out.println("cc="+currentChar);
				this.currentChar=(char)this.isreader.read();
			}
		}
		System.out.println("cc="+currentChar);
		double dnum=0,exp=0.1;
		if(Character.isDigit(currentChar)){
			dnum=Character.getNumericValue(currentChar);
			currentChar=(char)this.isreader.read();
			while(Character.isDigit(currentChar)){
				dnum=(dnum*10)+Character.getNumericValue(currentChar);
				currentChar=(char)this.isreader.read();
			}
			if(currentChar=='.'){
				currentChar=(char)this.isreader.read();
				if(Character.isDigit(currentChar)){
					dnum=dnum+(exp*Character.getNumericValue(currentChar));
					currentChar=(char)this.isreader.read();
					while(Character.isDigit(currentChar)){
						exp=exp*0.1;
						dnum=dnum+(exp*Character.getNumericValue(currentChar));
						currentChar=(char)this.isreader.read();
					}
				}
			}
			return dnum;
		}
		else
			throw new Exception("No numeric character");
	}
	
	private boolean isSeparator(char c){
		return (Character.isWhitespace(c) && c!='\r' && c!='\n');
	}
	
	/*
	public static void main(String[] args){
		try{
			INScanner inscanner = new INScanner(new File("./graph.dat"));
			System.out.print("nextInt:"+inscanner.nextInt()+",newline");
			inscanner.nextLine();System.out.println();
			System.out.println("next="+inscanner.next()+",nextDouble="+inscanner.nextDouble()+",nextDouble="+inscanner.nextDouble()+",nextInt="+inscanner.nextInt());
			System.out.println("test="+(0.01*(0.1)));
		} catch(Exception e){
			e.printStackTrace();
		}
	}
	*/
}
